/*
MIT License

Copyright (c) 2025 sidianneng(bbear_mail@163.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "motor_ctl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include <stdio.h>

#define L_MOTOR_A 20 
#define L_MOTOR_B 21
#define R_MOTOR_A 22
#define R_MOTOR_B 23

QueueHandle_t motor_ctl_queue = NULL;

static void motor_ctl_task(void* arg)
{
    uint32_t motordata;
    for(;;) {
        if(xQueueReceive(motor_ctl_queue, &motordata, pdMS_TO_TICKS(10))) {
            motor_ctl((M_Dir)(motordata >> 24 & 0x01), motordata >> 16 & 0xff, \
                      (M_Dir)(motordata >>  8 & 0x01), motordata & 0xff);
        } else {
            motor_ctl(M_STOP, 0, M_STOP, 0);
        }
    }
}

esp_err_t motor_init(void)
{
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = ((1ULL<<L_MOTOR_A) | (1ULL<<L_MOTOR_B) | (1ULL<<R_MOTOR_A) | (1ULL<<R_MOTOR_B));
    io_conf.pull_down_en = (gpio_pulldown_t)1;
    io_conf.pull_up_en = (gpio_pullup_t)0;
    gpio_config(&io_conf);

    gpio_set_level(L_MOTOR_A, 0);
    gpio_set_level(L_MOTOR_B, 0);
    gpio_set_level(R_MOTOR_A, 0);
    gpio_set_level(R_MOTOR_B, 0);

    motor_ctl_queue = xQueueCreate(64, sizeof(uint32_t));
    xTaskCreate(motor_ctl_task, "motor_ctl_task", 4096, NULL, 10, NULL);

    return ESP_OK;
}

esp_err_t motor_ctl(M_Dir l_dir, uint8_t leftspeed, M_Dir r_dir, uint8_t rightspeed)
{
    //printf("ldir:%d, lspd:%d rdir:%d rspd:%d\n", (int)l_dir, (int)leftspeed, (int)r_dir, (int)rightspeed);
    if(l_dir == M_FORWARD) {
        gpio_set_level(L_MOTOR_A, 0); 
        gpio_set_level(L_MOTOR_B, 1); 
    } else if(l_dir ==M_BACKWARD) {
        gpio_set_level(L_MOTOR_A, 1); 
        gpio_set_level(L_MOTOR_B, 0); 
    } else {
        gpio_set_level(L_MOTOR_A, 0); 
        gpio_set_level(L_MOTOR_B, 0); 
    }


    if(r_dir == M_FORWARD) {
        gpio_set_level(R_MOTOR_A, 0); 
        gpio_set_level(R_MOTOR_B, 1); 
    } else if(r_dir == M_BACKWARD) {
        gpio_set_level(R_MOTOR_A, 1); 
        gpio_set_level(R_MOTOR_B, 0); 
    } else {
        gpio_set_level(R_MOTOR_A, 0); 
        gpio_set_level(R_MOTOR_B, 0); 
    }

    return ESP_OK;
}

